﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rocket.Core
{
    public class EnumHelper
    {
        public static List<T> EnumToList<T>()
        {
            Type enumType = typeof(T);

            // Can't use type constraints on value types, so have to do check like this

            if (enumType.BaseType != typeof(Enum))

                throw new ArgumentException("T must be of type System.Enum");

            return new List<T>(Enum.GetValues(enumType) as IEnumerable<T>);
        }

        public static List<KeyValuePair<int, string>> EnumToKeyValueList<T>()
        {
            Type enumType = typeof(T);

            // Can't use type constraints on value types, so have to do check like this

            if (enumType.BaseType != typeof(Enum))

                throw new ArgumentException("T must be of type System.Enum");

            List<T> items = new List<T>(Enum.GetValues(enumType) as IEnumerable<T>);
            List<KeyValuePair<int, string>> enumList = new List<KeyValuePair<int, string>>();
            foreach (T item in items)
            {
                enumList.Add(new KeyValuePair<int,string>(Convert.ToInt32(item), item.ToString()));
            }
            return enumList;
        }
    }
}
